package diy.util
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import mx.graphics.codec.PNGEncoder;

	public class UploadUtil
	{
		public function UploadUtil()
		{
		}
		public static function upload(url:String,variables:URLVariables=null,sucessFun:Function=null,errorFun:Function=null):void{
			var uploader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest();
			request.url = url;
			request.data = variables;
			request.method = URLRequestMethod.POST;
			if(sucessFun != null)
				uploader.addEventListener(Event.COMPLETE, sucessFun);
			if(errorFun != null)
				uploader.addEventListener(IOErrorEvent.IO_ERROR, errorFun);
			uploader.load(request);
		}
		public static function bmpToStr(data:BitmapData):String{
			var pngEncoder:PNGEncoder = new PNGEncoder();
			var bmpScreencap:BitmapData = data;
			var xPixel:int=0;
			var yPixel:int=0;
			var pointColor:String;
			var newFxiedPointColor:String;
			var mpluses:int=0;
			var str:String = "";
			for(xPixel =0; xPixel < bmpScreencap.width; xPixel ++){ 
				for(yPixel =0; yPixel < bmpScreencap.height; yPixel ++){    
					pointColor = bmpScreencap.getPixel(xPixel, yPixel).toString(16);//获取每个象素的Hex值 
					if(bmpScreencap.getPixel32(xPixel, yPixel) == 0){
						str += "000000-1,";
						continue;
					}
					newFxiedPointColor =pointColor ;         
					for (mpluses=0;mpluses < 6 - pointColor.length;mpluses++){
						newFxiedPointColor="0" + newFxiedPointColor;  //不足6位要补足        
					} 
					str += newFxiedPointColor +  ","; //我这里用逗号分隔每个象素的颜色值    
				}
			}
			return str;
		}
	}
}